package servlet;

import dao.entity.MerchantDaoImpl;
import tools.UploadFileTool;

import javax.servlet.ServletException;
import javax.servlet.annotation.MultipartConfig;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.Part;
import java.io.File;
import java.io.IOException;

@WebServlet("/certificate")
@MultipartConfig(fileSizeThreshold = 1024*1024*2,//2MB
                 maxFileSize = 1024*1024*10,//10MB
                 maxRequestSize = 1024*1024*50)//50MB
public class MerchantCertificateServlet extends HttpServlet {
//    public static final String SAVE_DIRECTORY="certifi_imgs";
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String savePath=new UploadFileTool().UploadImage(request,"certifi_imgs");
        String mname=request.getParameter("mname");
        String m_phone=request.getParameter("m_phone");
        String m_intro=request.getParameter("m_intro");
        MerchantDaoImpl mdi=new MerchantDaoImpl();
        boolean is_online=mdi.isOnline(mname);
        if(is_online){
            //您已用此注册文件注册了店铺，请勿重复操作-0
            request.setAttribute("error_code",0);
            request.getRequestDispatcher("certificate.jsp").forward(request,response);
            return;
        }
        int n=mdi.addMerchant(mname,savePath,m_phone,m_intro);
        if(n!=0){
            request.setAttribute("error_code",1);//店铺注册成功
            request.setAttribute("m_intro",m_intro);// .setAttribute("m_intro",m_intro);
            request.getRequestDispatcher("store.jsp").forward(request,response);
        }else{
            request.setAttribute("error_code",2);//店铺注册失败
            request.getRequestDispatcher("certificate.jsp").forward(request,response);
        }
    }
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//        super.doGet(req, resp);
    }

}
